import React from 'react';
import {Row, Col, Button, Alert, notification} from 'antd';
import {dbConfirm} from "../../services/showServer";


const openNotificationWithIcon = type => () => (
  notification[type]({
    message: 'Error',
    description: '状态确认失败，请稍后重试！',
  })
);

class DBInfoComtainer extends React.Component {
  constructor(props) {
    super(props);

    this.state = {};
  }

  getTime(ver) {
    var date = new Date(ver);
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y + M + D + h + m + s;
  }

  onConfirmClick() {
    dbConfirm(this.props.db.id).then(function (res) {
      if (res.status == 200) {
        //刷新左侧树形结构
        this.props.signal.dispatch(this.props.db.id)
      } else {
        this.openNotificationWithIcon('error')
      }
    }.bind(this));
  }

  render() {
    const DBInfoRender = data => {

      var mess = "";
      var mtype = "";

      if (data.status == 11) {
        mess = "新增消息";
        mtype = "success";
      } else if (data.status == 12) {
        mess = "修改消息";
        mtype = "info";
      } else if (data.status == 13) {
        mess = "删除消息";
        mtype = "error";
      } else if (data.status == 22) {
        mess = "受影响";
        mtype = "warning";
      } else {
        return (<Row></Row>);
      }
      return (
        <Row>
          <Col span={24}>
            <Alert
              message={mess}
              description={data.info}
              type={mtype}
            />
          </Col>
          <Col span={24}>
            <Button type="primary" onClick={this.onConfirmClick.bind(this)}>状态确认</Button>
          </Col>
        </Row>
      )

    };

    const DBInfo = DBInfoRender(this.props.db);

    return (
      <div>
        <Row>
          <Col span={24} style={{textAlign: "center"}}>数据库信息</Col>
        </Row>
        <p></p>
        <Row>
          <Col span={3} style={{textAlign: "right"}}>数据库：</Col>
          <Col span={12}>{this.props.db.name}</Col>
        </Row>
        <p></p>
        <Row>
          <Col span={3} style={{textAlign: "right"}}>数据库编号：</Col>
          <Col span={12}>{this.props.db.id}</Col>
        </Row>
        <p></p>
        <Row>
          <Col span={3} style={{textAlign: "right"}}>全路径：</Col>
          <Col span={12}>{this.props.db.path}</Col>
        </Row>
        <p></p>
        <Row>
          <Col span={3} style={{textAlign: "right"}}>更新时间：</Col>
          <Col span={12}>{this.getTime(this.props.db.version)}</Col>
        </Row>
        <p></p>
        <p></p>
        <p></p>

        {DBInfo}
      </div>
    );
  }
}

export default DBInfoComtainer;

